<template>
  <div id="Active">
    <div class="Active-table">
      <el-table :data="tableData"
                @cell-dblclick='dblclickCell'
                style="width: 100%">
        <el-table-column prop="goods_name"
                         label="题库名">
        </el-table-column>
        <el-table-column prop="type"
                         align='center'
                         label="激活类型"
                         width="180">
        </el-table-column>
        <el-table-column prop="create_time"
                         align='center'
                         label="激活时间"
                         width="180">
        </el-table-column>
        <el-table-column prop="end_time"
                         align='center'
                         label="到期时间"
                         width="180">
        </el-table-column>
        <el-table-column prop="num"
                         align='center'
                         label="题数"
                         width="80">
        </el-table-column>
        <el-table-column align='center'
                         label="操作"
                         width="80">
          <template slot-scope="scope">
            <!-- <el-button @click="handleClick(scope.row)"
                       type="text"
                       size="small">查看</el-button> -->
            <span class="del"
                  @click="delItem(scope.row)">删除</span>
          </template>

        </el-table-column>
      </el-table>
    </div>
    <!-- 翻页按钮 -->
    <div class="Info-btn">
      <button @click="next_page(-1)">上一页</button>
      <button @click="next_page(1)">下一页</button>
    </div>
  </div>
</template>

<script>
import util from '../../../api/utils.js'

export default {
  inject: ['reload'],
  name: 'Active',
  data () {
    return {
      tableData: [],
      // 页数
      pages: 1,
      // 旧页数
      oldPage: null,
      // 最后一页时的判断
      backStatus: false
    }
  },

  components: {},

  computed: {},

  mounted () {
    this.$http.active({
      phone: localStorage.getItem('mobile'),
      listRows: 12,
      page: this.pages
    }).then((res) => {
      let data = res.data.data.data
      for (let item of data) {
        // 改变类型
        switch (item.type) {
          case 1:
            item.type = '永久'
            break
          case 2:
            item.type = '1年'
            break
        }
        // 改变激活时间
        item.create_time = util.formatDate(item.create_time * 1000)
        // 改变到期时间
        if (item.end_time) {
          item.end_time = util.formatDate(item.end_time * 1000)
        }
      }
      this.tableData = data
    }).catch((err) => {
      console.log(err)
    })
  },

  methods: {
    req_data (page) {
      if (page > 0) {
        this.$http.active({
          phone: localStorage.getItem('mobile'),
          listRows: 12,
          page: page
        }).then((res) => {
          if (res.data.data.data.length !== 0) {
            let data = res.data.data.data
            for (let item of data) {
              // 改变类型
              switch (item.type) {
                case 1:
                  item.type = '永久'
                  break
                case 2:
                  item.type = '1年'
                  break
              }
              // 改变激活时间
              item.create_time = util.formatDate(item.create_time * 1000)
              // 改变到期时间
              if (item.end_time) {
                item.end_time = util.formatDate(item.end_time * 1000)
              }
            }
            this.tableData = data
            if (data.length < 12) {
              this.backStatus = true
            }
          } else {
            this.pages = this.oldOage
            this.backStatus = true
            this.$message({
              message: '已经翻完了',
              type: 'warning'
            })
          }
        }).catch((err) => {
          console.log(err)
        })
      } else {
        this.pages = 1
        this.$message({
          message: '已经翻到顶部',
          type: 'warning'
        })
      }
    },
    // 翻页
    next_page (num) {
      if (this.backStatus) {
        if (num > 0) {
          this.$message({
            message: '已经翻完了',
            type: 'warning'
          })
        } else {
          this.backStatus = false
          this.pages = this.pages + num
          this.req_data(this.pages)
        }
      } else {
        this.oldPage = this.pages
        this.pages = this.pages + num
        this.req_data(this.pages)
      }
    },
    // 激活的题目中重新选择题库
    dblclickCell (row) {
      this.$router.push({ path: '/personalCenter/lately' })
      this.$store.state.centerNavI = '/personalCenter/lately'
      // // this.$store.state.navSel = 0
      this.$store.state.goodName = row.goods_name
      localStorage.setItem('gid', row.gid)
      this.$http.select({
        phone: localStorage.getItem('mobile'),
        id: row.gid
      }).then((res) => {
        // console.log(res)
      }).catch((err) => {
        console.log(err)
      })
    },
    // 删除
    delItem (row) {
      console.log('点击了')
      console.log(row)
      this.$confirm('此操作将在我的题库中删除该题库, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 隐藏题目
        this.$http.hide({
          id: row.gid,
          phone: localStorage.getItem('mobile')
        }).then((res) => {
          console.log(res)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.$http.active({
            phone: localStorage.getItem('mobile'),
            listRows: 12,
            page: this.pages
          }).then((res) => {
            let data = res.data.data.data
            for (let item of data) {
              // 改变类型
              switch (item.type) {
                case 1:
                  item.type = '永久'
                  break
                case 2:
                  item.type = '1年'
                  break
              }
              // 改变激活时间
              item.create_time = util.formatDate(item.create_time * 1000)
              // 改变到期时间
              if (item.end_time) {
                item.end_time = util.formatDate(item.end_time * 1000)
              }
            }
            this.tableData = data
          }).catch((err) => {
            console.log(err)
          })
        }).catch((err) => {
          console.log(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}

</script>
<style lang='less'>
#Active {
  background: white;
  padding: 10px 20px 10px 50px;
  height: calc(100% - 20px);
  // 表格
  .Active-table {
    text-align: center;
    height: calc(100% - 80px);
    // overflow: hidden;
    .el-table--border {
      border: none;
    }
    .el-table--border td {
      border: none;
    }
    .el-table {
      height: 100%;
      td {
        border: none;
        .del {
          color: red;
          cursor: pointer;
        }
      }
    }
  }
  .Info-btn {
    // border-top: 1px solid #d9d9d9;
    height: 80px;
    text-align: right;
    line-height: 80px;
    button {
      background: rgba(238, 238, 238, 1);
      border-radius: 10px;
      border: none;
      padding: 8px 20px;
      color: #333;
      margin-left: 40px;
    }
    button:hover {
      background: rgb(165, 165, 165);
      color: white;
    }
  }
}
</style>
